<?php
/**
 * 使用方法
$table = new DatabaseSchema('member_level', []);
$table->removeColumn('test_11');
$table->removeColumn('test_22');
$table->save();
 */
namespace gdbase;
use Phinx\Db\Adapter\AdapterFactory;
use Phinx\Db\Table;
use think\Db;

/**
 * 数据库结构操作，结合 topthink/think-migration 库 和 Phinx一起使用，通常情况下安装 topthink/think-migration即可
 * Class DatabaseSchema
 * @package gdbase
 */
class DatabaseSchema extends Table {
    private $tableName = '';
    public function getAdapter() {
        $config = Db::connect()->getConfig();

        if (0 == $config['deploy']) {
            $dbConfig = [
                'adapter'      => $config['type'],
                'host'         => $config['hostname'],
                'name'         => $config['database'],
                'user'         => $config['username'],
                'pass'         => $config['password'],
                'port'         => $config['hostport'],
                'charset'      => $config['charset'],
                'table_prefix' => $config['prefix'],
            ];
        } else {
            $dbConfig = [
                'adapter'      => explode(',', $config['type'])[0],
                'host'         => explode(',', $config['hostname'])[0],
                'name'         => explode(',', $config['database'])[0],
                'user'         => explode(',', $config['username'])[0],
                'pass'         => explode(',', $config['password'])[0],
                'port'         => explode(',', $config['hostport'])[0],
                'charset'      => explode(',', $config['charset'])[0],
                'table_prefix' => explode(',', $config['prefix'])[0],
            ];
        }

        $adapter = AdapterFactory::instance()->getAdapter($dbConfig['adapter'], $dbConfig);

        if ($adapter->hasOption('table_prefix') || $adapter->hasOption('table_suffix')) {
            $adapter = AdapterFactory::instance()->getWrapper('prefix', $adapter);
        }

        return $adapter;
    }

    public function __construct($tableName, $options = array()) {
        $this->tableName = $tableName;
        return parent::__construct($tableName, $options, $this->getAdapter());
    }

    /**
     * 添加列
     * @param Table\Column|string $columnName
     * @param null $type
     * @param array $options
     * @return Table
     * @throws \Exception
     */
    public function addColumn($columnName, $type = null, $options = array()) {
        if ($this->hasColumn($columnName)) {
            exception(  '表 ' . $this->tableName . ' 的 ' .$columnName . ' 列已存在');
        }
        return parent::addColumn($columnName, $type, $options); // TODO: Change the autogenerated stub
    }

    /**
     * 删除列
     * @param string $columnName
     * @return Table
     * @throws \Exception
     */
    public function removeColumn($columnName) {
        if (!$this->hasColumn($columnName)) {
            exception(  '表 ' . $this->tableName . ' 的 ' .$columnName . ' 列不存在');
        }
        return parent::removeColumn($columnName); // TODO: Change the autogenerated stub
    }

    /**
     * 重命名列
     * @param string $oldName
     * @param string $newName
     * @return Table
     * @throws \Exception
     */
    public function renameColumn($oldName, $newName) {
        if (!$this->hasColumn($oldName)) {
            exception(  '表 ' . $this->tableName . ' 的 ' .$oldName . ' 列不存在');
        }
        return parent::renameColumn($oldName, $newName); // TODO: Change the autogenerated stub
    }

    /**
     * 变更列
     * @param string $columnName
     * @param Table\Column|string $newColumnType
     * @param array $options
     * @return Table
     * @throws \Exception
     */
    public function changeColumn($columnName, $newColumnType, $options = array()) {
        if (!$this->hasColumn($columnName)) {
            exception(  '表 ' . $this->tableName . ' 的 ' .$columnName . ' 列不存在');
        }
        return parent::changeColumn($columnName, $newColumnType, $options); // TODO: Change the autogenerated stub
    }
}